package org.jeecg.modules.lab.items.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.items.entity.LabItems;
import org.jeecg.modules.lab.items.entity.LabItemsDevice;
import org.jeecg.modules.lab.items.item.mapper.LabItemsDeviceMapper;
import org.jeecg.modules.lab.items.item.mapper.LabItemsMapper;
import org.jeecg.modules.lab.items.item.service.ILabItemsService;
import org.jeecg.modules.lab.items.vo.LabItemsBasisItemsPage;
import org.jeecg.modules.lab.items.vo.LabItemsBasisPage;
import org.jeecg.modules.lab.items.vo.LabItemsDeviceVo;
import org.jeecg.modules.lab.items.vo.LabItemsPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 实验项目信息表
 * @Author: jeecg-boot
 * @Date: 2023-07-20
 * @Version: V1.0
 */
@Service
public class LabItemsServiceImpl extends ServiceImpl<LabItemsMapper, LabItems> implements ILabItemsService {

    @Autowired
    private LabItemsMapper labItemsMapper;

    @Autowired
    private LabItemsDeviceMapper labItemsDeviceMapper;

    @Override
    public List<LabItems> getListByIds(String[] ids) {
        return labItemsMapper.getListByIds(ids);
    }

    @Override
    public List<LabItemsPage> getListItemsByPid(String pid) {
        return labItemsMapper.getListItemsByPid(pid);
    }

    @Override
    public List<LabItems> getAllData() {
        QueryWrapper<LabItems> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("type", "C");
        return labItemsMapper.selectList(objectQueryWrapper);
    }

    @Override
    public Integer selctAll(String entrustId) {
        return labItemsMapper.selctAll(entrustId);
    }

    @Override
    public List<LabItemsBasisPage> getItemsLits(String itemName, String team, String sortType) {
        return labItemsMapper.getItemsList(itemName, team, sortType);
    }

    @Override
    public List<LabItemsBasisItemsPage> getBasisItemsList(String sortId) {
        return labItemsMapper.getBasisItemsList(sortId);
    }

    @Override
    public List<LabItems> getItemsListByName(String newName) {
        return labItemsMapper.getItemsListByName(newName);
    }

    @Override
    public List<LabItems> getItemsTempList(String itemName, String userId) {
        return labItemsMapper.getItemsTempList(itemName, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean savaItems(LabItemsDeviceVo itemsDeviceVo) {
        LabItems labItems = new LabItems();
        BeanUtils.copyProperties(itemsDeviceVo, labItems);
        labItems.setSynFlag("0");
        labItemsMapper.insert(labItems);
        if("C".equals(labItems.getType())){
            String itemsId = labItems.getId();
            List<LabItemsDevice> itemsDeviceList = itemsDeviceVo.getItemsDeviceList();
            for (LabItemsDevice itemsDevice : itemsDeviceList) {
                itemsDevice.setItemId(itemsId);
                itemsDevice.setSynFlag("0");
                labItemsDeviceMapper.insert(itemsDevice);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateItems(LabItemsDeviceVo itemsDeviceVo) {
        LabItems labItems = new LabItems();
        BeanUtils.copyProperties(itemsDeviceVo, labItems);
        labItems.setSynFlag("1");
        labItemsMapper.updateById(labItems);
        if("C".equals(labItems.getType())){
            String itemsId = labItems.getId();
            //先删除
            QueryWrapper<LabItemsDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("item_id", itemsId);
            labItemsDeviceMapper.delete(queryWrapper);
            //后保存
            List<LabItemsDevice> itemsDeviceList = itemsDeviceVo.getItemsDeviceList();
            for (LabItemsDevice itemsDevice : itemsDeviceList) {
                itemsDevice.setItemId(itemsId);
                labItems.setSynFlag("0");
                labItemsDeviceMapper.insert(itemsDevice);
            }
        }
        return true;
    }


}
